<?php

namespace app\admin\model\share;

use think\Model;
use traits\model\SoftDelete;

class Cash extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'share_cash';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

        'pay_time_text'
    ];
    


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public  function getServiceMoney($cash)
    {
        if($cash['service_charge'] == 0){
            $price = $cash['price'];
        }else{
            $cashPrice = floatval($cash['price']);
            $price = $cashPrice * (100 - $cash['service_charge']) / 100;
        }
        return round($price, 2);
    }

    public function users()
    {
        return $this->belongsTo('app\admin\model\Users', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
